/*******************************************************************************
 *	                           bsp cpu.c
 *******************************************************************************/
#include "bsp.h"
#include "mytype.h"
#include "str_ext.h"

/*******************************************************************************
 * @brief  配置字地址: 用户系统数据区
 *******************************************************************************/
#define CFG_ROM_BASE 0x1FFFF800

/*******************************************************************************
 * @brief  配置值
 *******************************************************************************/
static const uint32_t cfg_req[] = {
	//           反码    值   反码   值
	dword_bmake(~0xFF, 0xFF, ~0xA5, 0xA5), // 复位及读保护
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // USER DAT
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH WP
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH WP

	dword_bmake(~0xFF, 0xFF, ~0xFE, 0xFE), // SRAM SIZE // 96 + 128 = 224KB
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // USER DAT
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // USER DAT
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // USER DAT

	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH ENC KEY
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH ENC KEY
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH ENC KEY
	dword_bmake(~0xFF, 0xFF, ~0xFF, 0xFF), // FLASH ENC KEY
	0};

/*******************************************************************************
 * @brief  检测并配置.
 *******************************************************************************/
void bsp_cpu_option_check(void)
{
	(void)cfg_req;

	int diff = 0;

	// 读取判断
	loop(3)
	{
		diff = 0;
		loop(12)
		{
			diff += inpw(CFG_ROM_BASE + index * 4) != cfg_req[index];
		}
		// 无差别则退出
		if (!diff)
			break;
	}

	// 确认修改
	if (diff)
	{
		//flash_unlock();

		//flash_user_system_data_erase();

		loop(24)
		{
			//flash_user_system_data_program(0x1FFFF800 + 2 * index, ((uu16 *)cfg_req)[index] & 0xFF);
		}

		//flash_lock();

		NVIC_SystemReset();
	}
}
